<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->model('ktig');
		$this->load->library('form_validation');
	}
	public function index()
	{
		$this->load->helper('url');
		$this->load->view('index');
	}
	public function login()
	{
		$this->load->helper('url');
		$this->form_validation->set_rules('user_name', 'User Name', 'required|xss_clean|callback_status_check');
		$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
	  
		if($this->form_validation->run() == FALSE)
		{
			$this->load->view('index');
		}
		else
		{
			$this->login_check();
		}
	}
	function login_check()
	{
		if($this->user_check() == "yes")
		{
			$data['user_name']  =  $this->input->post('user_name');
			//$data['password']  =  md5($this->input->post('password')); 
			$data['password']  =  $this->input->post('password');
			$ambil= $this->ktig->db_check();
			
			foreach($ambil as $row)
			{
				$data['id_user']  = $row->ID_USER;
				$data['user_name']  = $row->USERNAME;
				$data['role']  = $row->ROLE;
				$data['bagian']  = $row->BAGIAN;
				$data['departemen']  = $row->Departemen;
				$data['nik']  = $row->NIK;
				$data['jabatan']  = $row->Jabatan;
			}
			
			$newdata = array('id_user' => $data['id_user'], 'user_name' => $data['user_name'], 'role' => $data['role'], 'bagian' => $data['bagian'], 'departemen' => $data['departemen'], 'nik' => $data['nik'], 'jabatan' => $data['jabatan'], 'is_logged_in'=>true); 
			$this->session->set_userdata($newdata);
			$ambil=$this->ktig->db_check();
			
			redirect('land');
		} 
		else
		{
			//echo "Login gagal, harap periksa username dan password anda.";
			$this->load->view('index');
		} 
	}
	
	function user_check()
	{
		$data['username'] = $this->input->post('user_name'); 
		$data['pass'] = ($this->input->post('password')); 
		$data['hasil'] = $this->ktig->db_check(); 
		 
		if($data['hasil'] == null) {
		  return "no"; 
		} 
		else { 
		  return "yes"; 
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */